import pytest
import requests

from config import BASE_URL
from tools import read_json


class TestCourse:
    def setup_method(self):
        # 获取验证码
        response = requests.get(BASE_URL + "/api/captchaImage")
        self.uuid = response.json()["uuid"]
        url = BASE_URL + "/api/login"
        data = {
            "username": "admin",
            "password": "HM_2023_test",
            "code": "2",
            "uuid": self.uuid
        }
        response = requests.post(url, json=data)
        self.token = response.json()["token"]

    @pytest.mark.parametrize("name,subject,price,applicablePerson,info,code,msg", read_json("course.json"))
    def test_add_course(self, name, subject, price, applicablePerson, info, code, msg):
        url = BASE_URL + "/api/clues/course"
        headers = {
            "Authorization": self.token
        }
        data = {
            "name": name,
            "subject": subject,
            "price": price,
            "applicablePerson": applicablePerson,
            "info": info
        }
        response = requests.post(url, json=data, headers=headers)
        print(response.json())
        assert response.status_code == 200
        assert response.json()["code"] == code
        assert msg in response.json()["msg"]
